// ==UserScript==
// @name         CSDN优化
// @namespace    https://greasyfork.org/users/228931-system
// @version      0.3.8
// @description  
// @author       System
// @match        *.csdn.net/*
// @icon         https://raw.gitcode.com/gagghik1/greyz4k/raw/main/csdn/icon.png
// @license      GPL-3.0-only
// @grant        GM_setValue
// @grant        GM_getValue
// @run-at       document-start
// @downloadURL  https://raw.gitcode.com/gagghik1/greyz4k/raw/main/csdn/meta.js
// @updateURL    https://raw.gitcode.com/gagghik1/greyz4k/raw/main/csdn/meta.js
// ==/UserScript==

(function() {
    'use strict';
    
    // 页面清洗核心样式 - 使用更具体的选择器和!important
    function genstyle() {
        return `
            /* 隐藏冗余元素：广告、登录提示、工具栏等 */
            #csdn-toolbar-write,.csdn-side-toolbar>:not(.option-box),
            .passport-login-tip-container,[class*=advert],.hide-preCode-box,.recommend-nps-box,
            .readall_box,.comment_read_more_box,.btn_open_app_prompt_div,.feed-Sign-span,
            .search-tag-box,.aside-header-fixed,.wap-shadowbox,.article-search-tip,#csdn-redpack,
            .csdn-reapck-select,.redpack-select-back,.toolbar-advert-default,.passport-login-container,
            [class^='banner-ad'],[id^='kp_box'],.post_feed_box,.signin,.more-toolbox,
            .right_box,div.container>nav,div.login-box,div.enterprise_blog,
            div.recommend-box,div.hide-article-box,aside,div#rightAside,div.write-guide,div.login-mark,
            #operate {
                display: none !important;
            }
            
            /* 新增：使用更具体的选择器确保隐藏APP/小程序提示 */
            body .feed-Sign-weixin,
            body #feed-open-app-btn,
            body #feed-Sign-weixin-btn {
                display: none !important;
                opacity: 0 !important;
                visibility: hidden !important;
                height: 0 !important;
                width: 0 !important;
                overflow: hidden !important;
                position: absolute !important;
                z-index: -1 !important;
            }
            
            /* 内容区域优化：自动展开 */
            div.comment-list-box{max-height: unset !important;}
            div.article_content{height: auto !important;overflow:auto !important;}
            pre{height: auto !important;}
            main{margin: 0 auto !important;float: none !important;}
            main *,#main *{ max-height: unset !important;}
            #main{margin-top:0 !important;}
            body{min-width: 100% !important;}
            
            /* 响应式优化 */
            @media screen and (max-width: 1200px) {
                .csdn-toolbar,.bottom-pub-footer {
                    display: none !important;
                }
            }
            
            /* 免登录复制优化 */
            code, code * {
                user-select: text !important;
            }
        `;
    }
    
    // 注入样式
    const style = document.createElement('style');
    document.documentElement.append(style);
    style.innerHTML = genstyle();
    
    // 免登录复制功能
    document.addEventListener('copy', e => e.stopImmediatePropagation(), true);
    
    // 去除搜索词链接（监听DOM变化）
    const querySelectorAllAndSelf = (target, selector) => [
        ...(target.matches(selector) ? [target] : []),
        ...target.querySelectorAll(selector)
    ];
    
    new MutationObserver(mutations => {
        mutations.forEach(record => {
            record.addedNodes.forEach(target => {
                if (target.nodeType === 1) {
                    querySelectorAllAndSelf(target, 'a[href*="so.csdn.net"]').forEach(link => {
                        link.replaceWith(link.textContent);
                    });
                }
            });
        });
    }).observe(document.documentElement, { subtree: true, childList: true });
    
    // 新增：确保页面加载后再次检查并隐藏元素
    window.addEventListener('load', () => {
        const hideElements = () => {
            document.querySelectorAll('.feed-Sign-weixin, #feed-open-app-btn, #feed-Sign-weixin-btn').forEach(el => {
                el.style.display = 'none';
                el.style.opacity = '0';
                el.style.visibility = 'hidden';
            });
        };
        
        // 初始隐藏
        hideElements();
        
        // 定时检查，防止动态加载
        setInterval(hideElements, 1000);
    });
})();